import {
  request,
  validator
} from '../../../utils/requestUtils';
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    form:{
      id:'',
      remark:'',
    },
    autosize:{ maxHeight: 200, minHeight: 50 }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showhandler(data) {
      this.setData({
        show: true,
        form:data
      })
      if (data.fileIds) {
        this.selectComponent('#files').initListHandler(data.fileIds.split(',').map(v => {
          return {
            url: v
          }
        }))
      }else{
        this.selectComponent('#files').initListHandler([])
      }
    },
    // 关闭弹窗
    onClose() {
      this.setData({
        show: false
      })
    },
    // 输入框值修改
    onChange(event) {
      switch (event.currentTarget.dataset['input']) {
        case 'remark':
          this.setData({
            "form.remark": event.detail,
          })
          break;
      }
    }, 
    
    // 提交订单
  async submitOrder(e) {
    console.log('e',e)
    if(e.currentTarget.dataset.status == 3 && !this.data.form.remark){
      wx.showToast({
        title: '请填写拒绝原因',
        icon: 'none'
      })
      return
    }
    this.setData({
      'form.orderStatus': e.currentTarget.dataset.status
    })
    let postMpdel = JSON.parse(JSON.stringify(this.data.form))
    const { valid } = await this.validatorAddress(postMpdel)
    let picList = this.selectComponent('#files').data.fileList
    if (picList && picList.length > 0) {
      this.setData({
        'form.fileIds': picList.map(v => v.url).join(',')
      })
    }
    if (!valid) return
    const result = await request({
      url: `/merchant/merchant_return_order`,
      method: "PUT",
      data: {
        id: this.data.form.id,
        orderStatus: this.data.form.orderStatus,
        remark: this.data.form.remark,
        fileIds: this.data.form.fileIds,
        returnType: this.data.form.returnType
      }
    })
    console.log('result.data.code', result.data.code)
    if (result.data.code == 200) {
      console.log('跳转', result.data.code)

      wx.switchTab({
        url: '/pages/merchant_return_order_list/index',
      })
    }
  },
  // 数据校验
  validatorAddress(params) {
    console.log('params', params)
    const rules = {}
    return validator(params, rules)
  }
  }
})